﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using FindIt.BLL;

namespace FindIt.UI.Pages
{
    public partial class Captcha : System.Web.UI.Page
    {
        private static double creditChangeForBadVote = double.Parse(ConfigurationManager.AppSettings["creditChangeForBadVote"]);
        private static double warningCredit = double.Parse(ConfigurationManager.AppSettings["warningCredit"]);
        private static double removalCredit = double.Parse(ConfigurationManager.AppSettings["removalCredit"]);
        private string imagesFolderPath = "~/Images/";
        private int rand;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string unknownImage = FindIt.BLL.Image.GetUnknownCaptchaImage();
                string knownImage = FindIt.BLL.Image.GetKnownCaptchaImage();
                string unknownImageURL = imagesFolderPath + unknownImage;
                string knownImageURL = imagesFolderPath + knownImage;

                Random random = new Random();
                rand = random.Next(1, 2000) % 2;

                switch (rand)
                {
                    // LEFT: Unknown | RIGHT: Known
                    case 0:
                        imgCaptchaLeft.ImageUrl = unknownImageURL;
                        imgCaptchaRight.ImageUrl = knownImageURL;
                        break;

                    // LEFT: Known | RIGHT: Unknown
                    case 1:
                        imgCaptchaLeft.ImageUrl = knownImageURL;
                        imgCaptchaRight.ImageUrl = unknownImageURL;
                        break;
                }

                ViewState["rand"] = rand;
                ViewState["unknownImage"] = unknownImage;
                ViewState["knownImage"] = knownImage;
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            int rand = int.Parse(ViewState["rand"].ToString());
            string unknownImageURL = ViewState["unknownImage"].ToString();
            string knownImageURL = ViewState["knownImage"].ToString();
            //string username = Session["username"].ToString();
            string keyLeft = txtCaptchaLeft.Text;
            string keyRight = txtCaptchaRight.Text;
            string query = Request.QueryString["q"];
            string queryType = Request.QueryString["t"];

            switch (rand)
            {
                // LEFT: Unknown | RIGHT: Known
                case 0:
                    if (FindIt.BLL.Image.CheckIfPermanentKeyword(knownImageURL, keyRight))
                    {
                        FindIt.BLL.Image.SaveTentativeKeyword(unknownImageURL, keyLeft, "febmoi");
                        Response.Redirect("WebPageResults.aspx?q=" + query + "&t=" + queryType);
                    }

                    else
                    {
                        string username = "febmoi";
                        PrivilegedUser.ChangeCredit(username, creditChangeForBadVote);
                        PrivilegedUser pu = PrivilegedUser.GetPUByUsername(username);

                        if (pu.Credit <= warningCredit && pu.Credit >= removalCredit)
                        {
                            pu.Warn();
                        }

                        else if (pu.Credit < removalCredit)
                        {
                            pu.Remove();
                        }

                        Response.Redirect("Captcha.aspx");
                    }

                    break;

                // LEFT: Known | RIGHT: Unknown
                case 1:
                    if (FindIt.BLL.Image.CheckIfPermanentKeyword(knownImageURL, keyLeft))
                    {
                        FindIt.BLL.Image.SaveTentativeKeyword(unknownImageURL, keyRight, "febmoi");
                        Response.Redirect("WebPageResults.aspx?q=" + query + "&t=" + queryType);
                    }

                    else
                    {
                        string username = "febmoi";
                        PrivilegedUser.ChangeCredit(username, creditChangeForBadVote);
                        PrivilegedUser pu = PrivilegedUser.GetPUByUsername(username);

                        if (pu.Credit <= warningCredit && pu.Credit >= removalCredit)
                        {
                            pu.Warn();
                        }

                        else if (pu.Credit < removalCredit)
                        {
                            pu.Remove();
                        }
                        Response.Redirect("Captcha.aspx");
                    }

                    break;
            }
        }
    }
}
